package com.example.administrator.androidros.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.example.administrator.androidros.R;
import com.example.administrator.androidros.base.BaseActivity;
import com.example.administrator.androidros.model.MemberModel;
import com.example.administrator.androidros.net.HttpUtils;
import com.example.administrator.androidros.utils.AgentConstants;
import com.example.administrator.androidros.utils.AgentSharedPreferences;
import com.example.administrator.androidros.utils.ServerConstants;
import com.example.administrator.androidros.widget.CustomTipDialog;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;

public class MyInfoActivity extends BaseActivity {
    @BindView(R.id.activity_my_info_et_member_name)
    protected EditText activity_my_info_et_member_name;
    @BindView(R.id.activity_my_info_radio_button_male)
    protected RadioButton activity_my_info_radio_button_male;
    @BindView(R.id.activity_my_info_radio_button_female)
    protected RadioButton activity_my_info_radio_button_female;
    @BindView(R.id.activity_my_info_tv_member_birthday)
    protected TextView activity_my_info_tv_member_birthday;
    private MemberModel memberModel = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_my_info);
    }

    @Override
    protected void initData() {
        initView();
    }

    @Override
    protected void onFailure(int requestCode, Object object) {
        showToast(R.string.common_response_failure);
        stopTipDialog();
    }

    @Override
    protected void onSuccess(int requestCode, Object object) {
        stopTipDialog();
        if (object != null) {
            switch (requestCode) {
                case 1001:
                    if (HttpUtils.getResultCode(object).equals(AgentConstants.RESULT_CODE_OK)) {
                        showToast(HttpUtils.getResultMessage(object));
                        //返回我的页面要更新
                        MemberModel memberModel = (MemberModel) HttpUtils.getData(object, MemberModel.class);
                        AgentSharedPreferences.saveMemberInfo(mBaseActivity, memberModel);
                        finish();
                    } else {
                        showToast(HttpUtils.getResultMessage(object));
                    }
                    break;
            }
        }
    }

    protected void initView() {
        memberModel = AgentSharedPreferences.getMemberInfo(mBaseActivity);
        activity_my_info_et_member_name.setText(memberModel.getMemberName());
        if (memberModel.getMemberSex() != null && !memberModel.getMemberSex().equals("")) {
            if (memberModel.getMemberSex().equals("男")) {
                activity_my_info_radio_button_male.setChecked(true);
                activity_my_info_radio_button_female.setChecked(false);
            } else if (memberModel.getMemberSex().equals("女")) {
                activity_my_info_radio_button_male.setChecked(false);
                activity_my_info_radio_button_female.setChecked(true);
            } else {
                activity_my_info_radio_button_male.setChecked(false);
                activity_my_info_radio_button_female.setChecked(false);
            }
        }

        if (memberModel.getMemberBirthdate() != null && !memberModel.getMemberBirthdate().equals("")) {
            activity_my_info_tv_member_birthday.setText(memberModel.getMemberBirthdate());
        }
    }

    @OnClick(R.id.activity_my_info_img_back)
    protected void onBackClick() {
        finish();
    }

    @OnClick(R.id.activity_my_info_btn_save)
    protected void onSaveClick() {
        String memberName = activity_my_info_et_member_name.getText().toString();
        String memberSex = activity_my_info_radio_button_male.isChecked() ? "男" : "女";
        String memberBirthdate = activity_my_info_tv_member_birthday.getText().toString();
        doUpdateMyInfo(memberName, memberSex, memberBirthdate);
    }

    //更新个人信息
    protected void doUpdateMyInfo(String memberName, String memberSex, String memberBirthdate) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("memberTelephone", AgentSharedPreferences.getMemberInfo(mBaseActivity).getMemberTelephone());
        jsonObject.put("memberName", memberName);
        jsonObject.put("memberSex", memberSex);
        jsonObject.put("memberBirthdate", memberBirthdate);
        HttpUtils.post(this, 1001, ServerConstants.UPDATE_MEMBER_INFO, jsonObject.toString(), this);
        startTipDialog(CustomTipDialog.ICON_TYPE_LOADING, getString(R.string.common_updating), true);
    }

    @OnClick(R.id.activity_my_info_tv_member_birthday)
    protected void onTimePickerView() {
        hideSoftKeyboard(activity_my_info_et_member_name,this);
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //时间选择器
        TimePickerView pvTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                activity_my_info_tv_member_birthday.setText(sdf.format(date));
            }
        }).setRangDate(null, Calendar.getInstance()).build();
        pvTime.show();
    }
}
